import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.RenderingHints.Key;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import java.security.KeyStore;
import java.util.ArrayList;

import javax.management.openmbean.ArrayType;
import javax.swing.*;

public class Background extends JPanel implements Runnable {
	public Image imgbg;
	public JButton btReset;
	String img = "/Images/Ro.png";
	public int fps = 1000;
	public int speed = 2;
	public int keyLeft = KeyEvent.VK_LEFT;
	public int keyRight = KeyEvent.VK_RIGHT;
	public boolean Left = false;
	public boolean Right = false;
	public boolean Mouse = false;
	public boolean vit = false;
	public boolean ngan = false;
	public boolean ga = false;
	public boolean Running = false;
	public JLabel Ro, egg;
	public JLabel SmallEgg, SmallShit;
	Poultry poultry;
	public Thread game;
	public Display view;
	public ArrayList<Poultry> list = new ArrayList<Poultry>();
	public ArrayList<JLabel> listEgg = new ArrayList<JLabel>();
	public ArrayList<JLabel> listShit = new ArrayList<JLabel>();
	public boolean huongdan=true;
	public Background(Display view) {
		imgbg=new ImageIcon(getClass().getResource("/Images/bg.jpg")).getImage();
		this.view = view;
		Running = true;
		game = new Thread(this);
		game.start();
		egg = new JLabel();
		egg.setIcon(ScaleImage("/Images/egg.png", 23, 30));
		egg.setBounds(Main.width / 2 - 30, Main.height - Main.heightline + 18,
				41, 50);
		Ro = new JLabel();
		Ro.setIcon(new ImageIcon(getClass().getResource(img)));
		Ro.setBounds(0, Main.height - Main.heightline - 81, 100, 81);
		ShowEggShit();
		KeyPress(view);
		
		this.add(Ro);
		this.add(egg);
	}

	private void KeyPress(Display view) {
		view.addKeyListener(new KeyAdapter() {

			public void keyPressed(KeyEvent e) {
				super.keyPressed(e);
				if (e.getKeyCode() == keyRight) {
					Right = true;
				}
				if (e.getKeyCode() == keyLeft) {
					Left = true;
				}
			}

			public void keyReleased(KeyEvent e) {
				super.keyReleased(e);
				if (e.getKeyCode() == keyRight) {
					Right = false;
				}
				if (e.getKeyCode() == keyLeft) {
					Left = false;
				}
				if (e.getKeyCode() == KeyEvent.VK_V) {
					vit = true;
					huongdan=false;
				}
				if (e.getKeyCode() == KeyEvent.VK_N) {
					ngan = true;
					huongdan=false;
				}
				if (e.getKeyCode() == KeyEvent.VK_G) {
					ga = true;
					huongdan=false;
				}
			}
		});

		view.addMouseMotionListener(new MouseAdapter() {
			@Override
			public void mouseMoved(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseMoved(e);
				Mouse = true;
				x = e.getX();
			}
		});
		view.setFocusable(true);
	}

	private void ShowEggShit() {
		for (int i = 1; i <= 3; i++) {
			SmallEgg = new JLabel();
			SmallShit = new JLabel();
			SmallShit.setIcon(ScaleImage("/Images/shit.png", 41, 44));
			SmallShit.setBounds(Main.width - i * 45 - 4, Main.height
					- Main.heightline + 13, 41, 44);
			SmallEgg.setIcon(ScaleImage("/Images/egg.png", 36, 44));
			SmallEgg.setBounds((i - 1) * 40 + 4, Main.height - Main.heightline
					+ 13, 36, 44);
			listEgg.add(SmallEgg);
			listShit.add(SmallShit);
			SmallEgg.disable();
			SmallShit.disable();
			this.add(SmallEgg);
			this.add(SmallShit);
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		g.drawImage(imgbg, 0,0, null);
		g.setColor(Color.black);
		g.drawLine(0, Main.heightline, Main.width, Main.heightline);
		g.drawLine(0, Main.height - Main.heightline, Main.width, Main.height
				- Main.heightline);
		g.drawString("X", Main.width / 2, Main.height - 35);
		g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
		g.drawString(Integer.toString(Main.point), Main.width / 2 + 15,
				Main.height - 35);
		if(huongdan)
		{
			g.drawString("- Nhấn phím V để xuất hiện một con vịt",Main.width / 2 - 180, Main.height / 2 - 100);
			g.drawString("- Nhấn phím N để xuất hiện một con ngan",Main.width / 2 - 180, Main.height / 2 - 70);
			g.drawString("- Nhấn phím G để xuất hiện một con gà",Main.width / 2 - 180, Main.height / 2 - 40);
			g.drawString("- Thua khi không hứng được trứng 3 lần",Main.width / 2 - 180, Main.height / 2 - 10);
			g.drawString("- Thua khi hứng được shit 3 lần",Main.width / 2 - 180, Main.height / 2 + 20);
			g.drawString("- Tốc độ trứng rơi sẽ tăng khi ăn được 20 trứng",Main.width / 2 - 180, Main.height / 2 + 50);
			g.drawString("- Dùng chuột hoặc phím mũi tên di chuyển rổ qua lại",Main.width / 2 - 180, Main.height / 2 + 80);
		}
	}

	public int x = 0;

	@Override
	public void run() {
		while (Running) {
			
			if (Main.shitNum >= 3 || Main.eggNum >= 3) {
				for (int i = 0; i < list.size(); i++) {
					list.get(i).stop();
					this.remove(list.get(i).EggShit);
					this.remove(list.get(i));
				}
				ResetGame();
				this.repaint();
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				this.stop();
			}
			if (Main.eatShit) {
				Main.eatShit = false;
				if (Main.shitNum != 0)
					for (int i = 0; i < Main.shitNum; i++) {
						if (i >= listShit.size())
							break;
						listShit.get(i).enable();
						
					}
			}
			if (Main.MissEgg) {
				Main.MissEgg = false;

			}
			if (Right && Ro.getBounds().getMaxX() < Main.width) {
				Ro.setBounds(x, Main.height - Main.heightline - 81, 100, 81);
				x += 2;
			}
			if (Left && Ro.getBounds().getMinX() > 0) {
				Ro.setBounds(x, Main.height - Main.heightline - 81, 100, 81);
				x -= 2;
			}
			if (Mouse) {
				Ro.setBounds(x - Ro.getSize().width / 2, Main.height
						- Main.heightline - 81, 100, 81);
			}
			if (vit) {
				getPoultry("/Images/vit.png");
				vit = false;
			}
			if (ngan) {
				getPoultry("/Images/ngan.png");
				ngan = false;
			}
			if (ga) {
				getPoultry("/Images/ga.png");
				ga = false;
			}
			getFPS();
		}

	}

	public void EnableEgg() {
		for (int i = 0; i < Main.eggNum; i++) {
			if (i >= listEgg.size())
				break;
			listEgg.get(i).enable();
		}
	}

	private void getPoultry(String poultryURL) {
		poultry = new Poultry(poultryURL, this);
		list.add(poultry);
		this.add(poultry);
		this.repaint();
		System.out.println("vit");
	}

	public void getFPS() {
		try {
			game.sleep(fps / 1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void stop() {
		Running = false;
		try {
			this.game.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

	public ImageIcon ScaleImage(String img, int w, int h) {
		ImageIcon ii = new ImageIcon(getClass().getResource(img));
		Image i = ii.getImage();
		return new ImageIcon(i.getScaledInstance(w, h, Image.SCALE_SMOOTH));
	}


	private void ResetGame() {
		btReset = new JButton();
		btReset.setText("Hứng được có "+Main.point+" trứng thôi.Chơi lại nhé!!!");
		btReset.setBounds(Main.width / 2 - 140, Main.height / 2 - 35, 280, 70);
		btReset.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				newgame();
			}
		});
		this.add(btReset);
	}

	public void newgame() {
		
		System.out.println("new game");
		Main.point = 0;
		Main.eggNum = 0;
		Main.shitNum = 0;
		Main.eatShit = false;
		Main.MissEgg = false;
		Running = true;
		for (int i = 0; i <listEgg.size(); i++) {
			listEgg.get(i).disable();
			listShit.get(i).disable();
		}
		list.clear();
		game = new Thread(this);
		game.start();
		this.remove(btReset);
		this.repaint();
		
		
	}
}
